package tako.wisers.com.android.apis.adapter;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.activity.Tako;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.event.StationEvent;
import tako.wisers.com.android.apis.handler.Handler;
import tako.wisers.com.android.apis.handler.StationSwitchHandler;
import tako.wisers.com.android.apis.parser.ConfXMLParser;
import tako.wisers.com.android.apis.parser.StationXMLParser;
import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.parser.xml.item.Draw;
import tako.wisers.com.android.apis.util.Global;
import tako.wisers.com.android.apis.view.ViewNameIDConverse;

import android.content.res.TypedArray;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;

public class TopButtonBarAdapter extends BaseAdapter implements
		OnItemClickListener {
	private List<ButtonItem> mButtonList;
	private Tako mTako;
	private StationXMLParser mXmlParser;
	private EventBus mBus = EventBus.getInstance();
	private static final String MAIN_LAYOUT = "main";
	private int mCurrentTopIndex;

	public TopButtonBarAdapter(Tako tako) {
		try {
			mTako = tako;
			mXmlParser = StationXMLParser.getInstance();
			initButtonBar();
			TypedArray a = mTako.obtainStyledAttributes(R.styleable.Gallery);
			a.recycle();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	private void initButtonBar() throws SecurityException,
			NoSuchMethodException, ClassNotFoundException,
			IllegalArgumentException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		ConfXMLParser confXml = ConfXMLParser.getInstance();
		Map<String, Draw> drawMap = confXml.getDrawListByLayout(MAIN_LAYOUT);
		mButtonList = new ArrayList<ButtonItem>();

		for (ButtonItem item : mXmlParser.getStationMap().values()) {
			String drawName = item.getDrawName();
			if (drawMap != null && drawMap.containsKey(drawName)
					&& drawMap.get(drawName).getBackGround() != null) {
				Drawable arawable = new BitmapDrawable(drawMap.get(drawName)
						.getBackGround());
				item.setDrawable(arawable);
			} else {
				int drawid = ViewNameIDConverse.getIdFromName(drawName);
				if (drawid == 0) {
					item.setDrawable(mTako.getResources().getDrawable(
							R.drawable.unknow));
				} else {
					Drawable drawable = mTako.getResources()
							.getDrawable(drawid);
					item.setDrawable(drawable);
				}
			}
			Constructor<?> constructor = Class.forName(item.getHandler())
					.getConstructor(Tako.class);
			Handler handler = (Handler) constructor.newInstance(mTako);
			mBus.signinEvent(item.getId(), handler, Event.EVENT_CLICK);
			mButtonList.add(item);
		}
		if (mButtonList.size() > 0) {
			TextView text = (TextView) mTako.findViewById(R.id.type_info);
			text.setText(mButtonList.get(0).getName().toUpperCase());
		}
		System.gc();
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i = new ImageView(mTako);
		ButtonItem item = mButtonList.get(position % mButtonList.size());
		i.setImageDrawable(item.getDrawable());
		i.setPadding(12, 0, 12, 0);
		i.setScaleType(ScaleType.FIT_XY);
		i.setLayoutParams(new Gallery.LayoutParams(80, 40));
		return i;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id) {
		mCurrentTopIndex = Math.abs(position % mButtonList.size());
		ButtonItem item = mButtonList.get(mCurrentTopIndex);
		TextView text = (TextView) mTako.findViewById(R.id.type_info);
		text.setText(item.getName().toUpperCase());
		mTako.dialogShow(Global.READ_DIALOG);
		if (item.getHandler().equals(StationSwitchHandler.class.getName())) {
			StationEvent event = new StationEvent(item.getId(),
					Event.EVENT_CLICK, item.getType(), item.getSubtype(), item
							.getShop(), item.getLocation());
			mBus.executeEvent(event);
		} else {
			Event event = new Event(item.getId(), Event.EVENT_CLICK);
			mBus.executeEvent(event);
		}
	}

	public ButtonItem getCurrentTopButton() {
		return mButtonList.get(mCurrentTopIndex);
	}

}
